/*******************************************************************************
 * Copyright (c) 2011 Erik Byström.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package se.slackers.automatunes.audio;

import javax.sound.midi.Instrument;
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Patch;
import javax.sound.midi.Synthesizer;

/**
 * @author Erik Byström
 * 
 */
public class SoundSystem {
	private static final int BASE = 15;
	private static final int HIHAT = 14;

	private Synthesizer synthesizer;
	private MidiChannel[] channels;

	public SoundSystem() {
		init();
	}

	private boolean init() {
		if (synthesizer != null) {
			return true;
		}
		try {
			synthesizer = MidiSystem.getSynthesizer();
			synthesizer.open();
			channels = synthesizer.getChannels();

			changeIntrument(channels[0], "Vibraphone");
			changeIntrument(channels[HIHAT], "hat");
			changeIntrument(channels[BASE], "Syn Drum");
			return true;
		} catch (final Exception e) {
			System.err.println("Can't initialize sound system!");
			e.printStackTrace();
		}
		return false;
	}

	private void changeIntrument(final MidiChannel channel, final String query) {
		final Instrument[] instruments = synthesizer.getAvailableInstruments();
		for (final Instrument i : instruments) {
			final String name = i.getName().toLowerCase();
			if (name.indexOf(query.toLowerCase()) >= 0) {
				synthesizer.loadInstrument(i);
				final Patch patch = i.getPatch();
				channel.programChange(patch.getBank(), patch.getProgram());
				System.out.println("Using " + name);
				return;
			}
		}
		System.out.println("Nothing found");
	}

	public Playable channel(final int channel) {
		return new Playable() {
			@Override
			public void noteOn(final Note note) {
				SoundSystem.this.noteOn(channel, note);
			}

			@Override
			public void noteOff(final Note note) {
				SoundSystem.this.noteOff(channel, note);
			}
		};
	}

	public void noteOn(final int channel, final Note note) {
		if (!init()) {
			return;
		}
		channels[channel].noteOn(note.note, note.velocity);
	}

	public void noteOff(final int channel, final Note note) {
		if (!init()) {
			return;
		}
		channels[channel].noteOff(note.note);
	}

	public void allNotesOff(final int channel) {
		if (!init()) {
			return;
		}
		channels[channel].allNotesOff();
	}
}
